﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace Common.Net
{
    public class Request
    {
        private CookieContainer container;
        public Request(CookieContainer cont)
        {
            this.container = cont;
        }
        public CookieContainer GetPage(string url, string Encoding, out string result)
        {
            string content = "";
            // Create a new HttpWebRequest object.Make sure that
            // a default proxy is set if you are behind a fure wall.
            //其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的Create方法来创建.
            HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(url);
            myHttpWebRequest1.CookieContainer = new CookieContainer();
            //不维持与服务器的请求状态
            myHttpWebRequest1.KeepAlive = false;
            //创建一个HttpWebRequest对象
            //Assign the response object of HttpWebRequest to a HttpWebResponse variable.\
            HttpWebResponse myHttpWebResponse1;
            // 根据微软MSDN上所说:"决不要直接创建HttpWebResponse的实例,要使用HttpWebRequest的GetResponse()方法返回的实例."具体的原因我也不清楚,可能HttpWebResponse类的构造函数中没有实现HttpWebResponse实例的代码吧.
            myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();

            //设置页面的编码模式
            System.Text.Encoding utf8 = System.Text.Encoding.GetEncoding(Encoding);
            Stream streamResponse = myHttpWebResponse1.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse, utf8);
            Char[] readBuff = new Char[256];
            //这里使用了StreamReader的Read()方法,参数意指从0开始读取256个char到readByff中.
            //Read()方法返回值为指定的字符串数组,当达到文件或流的末尾使,方法返回0
            int count = streamRead.Read(readBuff, 0, 256);
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                content += outputData;
                count = streamRead.Read(readBuff, 0, 256);
            }
            result = content;
            myHttpWebResponse1.Close();
            return myHttpWebRequest1.CookieContainer;
        }

        /// <summary>
        /// get方式的URL请求都可以用这个方法
        /// </summary>
        /// <param name="rUrl"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public string ReadUrlContent(string rUrl, string Encode)
        {

            //用于作为读取内容操作的缓冲区
            byte[] buf = new byte[8010];
            // 请求该页面
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rUrl);
            request.CookieContainer = container;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //将读取到的数据放入到流里面
            Stream resStream = response.GetResponseStream();
            string tempString = "";
            int totalcount = 0;
            int count = 0;
            do
            {
                count = resStream.Read(buf, 0, buf.Length);
                if (count != 0)
                {
                    tempString += Encoding.GetEncoding(Encode).GetString(buf, 0, count).ToString();
                }

                totalcount += count;
            }
            while (count > 0);
            resStream.Close();
            return tempString;
        }


    }
}
